
package com.jf.cloud.unicomdata.task;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.util.DateUtils;
import com.jf.cloud.unicomdata.dto.HourDataDTO;
import com.jf.cloud.unicomdata.model.HourData;
import com.jf.cloud.unicomdata.service.HourDataService;
import com.jf.cloud.unicomdata.service.ScenicSpotService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 见 https://github.com/xuxueli/xxl-job
 * 开发步骤：
 * 1、任务开发：在Spring Bean实例中，开发Job方法；
 * 2、注解配置：为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")"，注解value值对应的是调度中心新建任务的JobHandler属性的值。
 * 3、执行日志：需要通过 "XxlJobHelper.log" 打印执行日志；
 * 4、任务结果：默认任务结果为 "成功" 状态，不需要主动设置；如有诉求，比如设置任务结果为失败，可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果；
 *
 * @author zz
 * @date 2021/04/23
 */
@Component
public class UnicomDataTask {
    private static final Logger LOG = LoggerFactory.getLogger(UnicomDataTask.class);
    @Autowired
    private HourDataService hourDataService;

    @Autowired
    private ScenicSpotService scenicSpotService;

    /**
     * 获取联通数据入库
     */
    @XxlJob("getUnicomData")
    public void getUnicomData() {
        XxlJobHelper.log("开始获取联通数据。。。");
        hourDataService.getUnicomData();
    }

    @XxlJob("getUnicomDataKill")
    public void getUnicomDataKill() {
        XxlJobHelper.log("开始获取联通数据。。。");
        Random random = new Random();
        // 生成随机整数
        String param = XxlJobHelper.getJobParam();
        int min = Integer.parseInt(param.split(",")[0]);
        int max = Integer.parseInt(param.split(",")[1]);
        int randomNumber = random.nextInt(max - min + 1) + min;
        Map<String, Object> dsmap = scenicSpotService.getDataByDongshan();
//        Map<String,Object> mmap= scenicSpotService.getDataByMiao();
        Integer dday = Integer.valueOf(dsmap.get("day").toString().split("：")[1].split("人")[0]);
//        Integer mday=Integer.valueOf(mmap.get("day").toString().split("：")[1].split("人")[0]);
        Integer dmonth = Integer.valueOf(dsmap.get("month").toString().split("：")[1].split("人")[0]);
//        Integer mmonth=Integer.valueOf(mmap.get("month").toString().split("：")[1].split("人")[0]);
        scenicSpotService.updateDongshan("今日到访：" + (dday + randomNumber) + "人", "本月到访：" + (dmonth + randomNumber) + "人");
//        scenicSpotService.updateMiao("今日到访："+(mday+randomNumber)+"人","本月到访："+(mmonth+randomNumber)+"人");
    }

    @XxlJob("dongshan")
    public void getUnicomDataKill1() {
        PageDTO pageDTO1 = new PageDTO();
        pageDTO1.setPageNum(1);
        pageDTO1.setPageSize(1);
        HourDataDTO hourDataDTO1 = new HourDataDTO();
        hourDataDTO1.setCode("074T211029144312N0008");
        //当前时间前2小时
        Date dayhour = DateUtils.getDateBeforeHour(new Date(), -2);
        hourDataDTO1.setDataTime(DateUtils.dateToMinuteNumber(dayhour));
        List<HourData> ls1 = hourDataService.page(pageDTO1, hourDataDTO1).getList();
        int dayNum = 0;
        if (ls1.size() > 0) {
            dayNum = ls1.get(0).getTotalNum();
        }
        LOG.info("*****东山日*******" + dayNum);
        //循环截止今日本月每天的23点总人数
        int totalNum = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        int dayNums = Integer.valueOf(sdf.format(new Date()));
        for (int i = 1; i < dayNums; i++) {
            PageDTO pageDTO = new PageDTO();
            pageDTO.setPageNum(1);
            pageDTO.setPageSize(1);
            HourDataDTO hourDataDTO = new HourDataDTO();
            hourDataDTO.setCode("074T211029144312N0008");
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd23");

            Calendar c1 = Calendar.getInstance();
            c1.setTime(new Date());
            c1.add(Calendar.DATE,-i);
            long ldate =Long.valueOf(simpleDateFormat.format(c1.getTime()));
            LOG.info("**********时间**" + ldate + "************");
            hourDataDTO.setDataTime(ldate);
            LOG.info("************" + hourDataDTO.toString() + "************");
            List<HourData> ls = hourDataService.page(pageDTO, hourDataDTO).getList();
            if (ls.size() > 0) {
                int dayTotalNum = ls.get(0).getTotalNum();
                totalNum += dayTotalNum;
            }

        }
        LOG.info("*******东山总*****" + totalNum + "************");

        scenicSpotService.updateDongshan("今日到访：" + dayNum + "人", "本月到访：" + (totalNum+dayNum) + "人");
    }

    @XxlJob("miao")
    public void getUnicomDataKill2() {
        PageDTO pageDTO1 = new PageDTO();
        pageDTO1.setPageNum(1);
        pageDTO1.setPageSize(1);
        HourDataDTO hourDataDTO1 = new HourDataDTO();
        hourDataDTO1.setCode("074T211029144312N0007");
        //当前时间前2小时
        Date dayhour = DateUtils.getDateBeforeHour(new Date(), -2);
        hourDataDTO1.setDataTime(DateUtils.dateToMinuteNumber(dayhour));
        List<HourData> ls1 = hourDataService.page(pageDTO1, hourDataDTO1).getList();
        int dayNum = 0;
        if (ls1.size() > 0) {
            dayNum = ls1.get(0).getTotalNum();
        }
        LOG.info("********日****" + dayNum + "************");
        //循环截止今日本月每天的23点总人数
        int totalNum = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        int dayNums = Integer.valueOf(sdf.format(new Date()));
        for (int i = 1; i < dayNums; i++) {
            PageDTO pageDTO = new PageDTO();
            pageDTO.setPageNum(1);
            pageDTO.setPageSize(1);
            HourDataDTO hourDataDTO = new HourDataDTO();
            hourDataDTO.setCode("074T211029144312N0007");
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd23");

            Calendar c1 = Calendar.getInstance();
            c1.setTime(new Date());
            c1.add(Calendar.DATE,-i);
            long ldate =Long.valueOf(simpleDateFormat.format(c1.getTime()));
            LOG.info("**********时间**" + ldate + "************");
            hourDataDTO.setDataTime(ldate);
            LOG.info("************" + hourDataDTO.toString() + "************");
            List<HourData> ls = hourDataService.page(pageDTO, hourDataDTO).getList();
            if (ls.size() > 0) {
                int dayTotalNum = ls.get(0).getTotalNum();
                totalNum += dayTotalNum;
            }

        }
        LOG.info("*****总*******" + totalNum + "************");
        scenicSpotService.updateMiao("今日到访：" + dayNum + "人", "本月到访：" + (totalNum+dayNum) + "人");
    }

    @XxlJob("getUnicomDataMiao")
    public void getUnicomDataMiao() {
        XxlJobHelper.log("开始获取联通数据。。。");
        Random random = new Random();
        // 生成随机整数
        String param = XxlJobHelper.getJobParam();
        int min = Integer.parseInt(param.split(",")[0]);
        int max = Integer.parseInt(param.split(",")[1]);
        int randomNumber = random.nextInt(max - min + 1) + min;
//        Map<String,Object> dsmap= scenicSpotService.getDataByDongshan();
        Map<String, Object> mmap = scenicSpotService.getDataByMiao();
//        Integer dday=Integer.valueOf(dsmap.get("day").toString().split("：")[1].split("人")[0]);
        Integer mday = Integer.valueOf(mmap.get("day").toString().split("：")[1].split("人")[0]);
//        Integer dmonth=Integer.valueOf(dsmap.get("month").toString().split("：")[1].split("人")[0]);
        Integer mmonth = Integer.valueOf(mmap.get("month").toString().split("：")[1].split("人")[0]);
//        scenicSpotService.updateDongshan("今日到访："+(dday+randomNumber)+"人","本月到访："+(dmonth+randomNumber)+"人");
        scenicSpotService.updateMiao("今日到访：" + (mday + randomNumber) + "人", "本月到访：" + (mmonth + randomNumber) + "人");
    }

    @XxlJob("getUnicomDataJiangNan")
    public void getUnicomDataJiangNan() {
        Random random = new Random();
        // 生成随机整数
        String param = XxlJobHelper.getJobParam();
        int min = Integer.parseInt(param.split(",")[0]);
        int max = Integer.parseInt(param.split(",")[1]);
        int randomNumber = random.nextInt(max - min + 1) + min;
        Map<String, Object> mmap = scenicSpotService.getDataByMaTou();
        Integer mday = Integer.valueOf(mmap.get("day").toString());
        Integer mmonth = Integer.valueOf(mmap.get("month").toString());
        scenicSpotService.updateMaTou( (mday + randomNumber)+"" ,  (mmonth + randomNumber)+"");
    }

    @XxlJob("updateUnicomDataKill")
    public void updateUnicomDataKill() {
        XxlJobHelper.log("开始更新LED数据。。。");
        scenicSpotService.updateDongshan("今日到访：0人", null);
        scenicSpotService.updateMiao("今日到访：0人", null);
        scenicSpotService.updateMaTou(0+"", null);

    }

    @XxlJob("updateMonthUnicomDataKill")
    public void updateMonthUnicomDataKill() {
        XxlJobHelper.log("开始更新LED数据。。。");
        scenicSpotService.updateDongshan("今日到访：0人", "本月到访：0人");
        scenicSpotService.updateMiao("今日到访：0人", "本月到访：0人");
        scenicSpotService.updateMaTou(0+"", 0+"");

    }


    @XxlJob("updateData")
    public void updateData() {
        String param = XxlJobHelper.getJobParam();
        int min = Integer.parseInt(param.split(",")[0]);
        int max = Integer.parseInt(param.split(",")[1]);
        Map<String, Object> dsmap = scenicSpotService.getDataByDongshan();
        Map<String, Object> mmap = scenicSpotService.getDataByMiao();
        Integer dday = Integer.valueOf(dsmap.get("day").toString().split("：")[1].split("人")[0]);
        Integer mday = Integer.valueOf(mmap.get("day").toString().split("：")[1].split("人")[0]);
        Integer dmonth = Integer.valueOf(dsmap.get("month").toString().split("：")[1].split("人")[0]);
        Integer mmonth = Integer.valueOf(mmap.get("month").toString().split("：")[1].split("人")[0]);
        XxlJobHelper.log("开始更新LED数据。。。");
        scenicSpotService.updateDongshan("今日到访：" + min + "人", "本月到访：" + (dmonth + min - dday) + "人");
        scenicSpotService.updateMiao("今日到访：" + max + "人", "本月到访：" + (mmonth + max - mday) + "人");
    }
}
